#include "scan.h"

int ch_scan(ch_fileinfo *file, short formats) {
	//printf("\t- Attempting to open file '%s'...\n", file->filename); // Make -v option
	FILE *fd = fopen(file->filename, "rb");
	int FILESIZE;

	// Does the file exist?
	if (fd == NULL) {
		file->could_find_file = 'n';
		printf("\t- Couldn't find file '%s'...\n", file->filename);
		return -1;
	}
	else
		file->could_find_file = 'y';

	fseek(fd, 0,SEEK_END);
	FILESIZE = ftell(fd);
	rewind(fd);

	printf("\t- Opened file '%s'... (%d bytes in size)\n", file->filename, FILESIZE); // make -v option
	// Ok file exists, start scan..

	// Get the jpgs..
	if (formats & GET_JPGS) {
		printf("\t- Scanning for jpg signatures...\n");
		file->jpg_headers = ch_algorithmS(fd, FILESIZE, JPG_HEADER, JPG_HEADER_SIZE, file->jpg_header_markers);
		file->jpg_footers = ch_algorithmS(fd, FILESIZE, JPG_FOOTER, JPG_FOOTER_SIZE, file->jpg_footer_markers);
		printf("\t\t- %d headers and %d footers found.\n", file->jpg_headers, file->jpg_footers);
		file->scanned_for_jpgs = 'y';
		// file->likely_jpgs = ch_softcore_scan(signature_byte_potitions)
	}
	
	if (formats & GET_GIFS) {
		printf("\t- Scanning for gif headers...\n");
		file->gif_headers = ch_algorithmS(fd, FILESIZE, GIF_HEADER, GIF_HEADER_SIZE, file->gif_header_markers);
		file->gif_footers = ch_algorithmS(fd, FILESIZE, GIF_FOOTER, GIF_FOOTER_SIZE, file->gif_footer_markers);
		printf("\t\t- %d headers and %d footers found.\n", file->gif_headers, file->gif_footers);
		file->scanned_for_gifs = 'y';
	}
	
	if (formats & GET_PNGS) {
		printf("\t- Scanning for png headers...\n");
		file->png_headers = ch_algorithmS(fd, FILESIZE, PNG_HEADER, PNG_HEADER_SIZE, file->png_header_markers);
		file->png_footers = ch_algorithmS(fd, FILESIZE, PNG_FOOTER, PNG_FOOTER_SIZE, file->png_footer_markers);
		printf("\t\t- %d headers and %d footers found.\n", file->png_headers, file->png_footers);
		file->scanned_for_pngs = 'y';
	}
	
	if (GET_BMPS & formats) { // this will be a bugger...
		printf("\t- bmp searching is not supported yet...\n");
	}
	
	// Fill in possible_xxxxx headers...
	file->possible_jpgs = (file->jpg_headers + file->jpg_footers)/2;
	file->possible_pngs = (file->png_headers + file->png_footers)/2;
	file->possible_gifs = (file->gif_headers + file->gif_footers)/2;
	file->possible_images = file->possible_gifs + file->possible_jpgs + file->possible_pngs;

	//printf("\t- Closing file '%s'...\n", file->filename); // make -v option
	fclose(fd);
	return 0;
}

int ch_algorithmS(FILE *data_fd, int data_size, char *signature_data, int signature_data_size, int *byte_array) {
	int x = 0, z = 0, signature_count = 0, i;
	char c;
	
	// Set up byte position array memory space...
	byte_array = (int*)malloc(sizeof(int));
	
	fread(&c, 1, 1, data_fd);
	for(i=0;i<data_size;i++) {
		if (x == 0) { // if this is the first (possible) occurance of signature[0]
			if (c == signature_data[0]) { // if the read char mathches the first char of the signature...
				x = 1; // tell the algorithm we are now waiting on the second character..
			}
			if (x == 1) { // if the previously read char was, indeed, the first char of signature[]
				for (z=0;z<signature_data_size;z++) { // continue to read chars until either the read char does NOT match signature[], or we reach the end of signature[]
					fread(&c, 1, 1, data_fd);
					if (x < signature_data_size) { // if we're still looping and matching signature[]..
						if (c == signature_data[x]) {
							x++;
						}
						else { // uh oh :( didn't match signature[]... reset x, read, and break
							x = 0;
							break;
						}
					}
					else if (x == signature_data_size) { // We've found a matching signature!!
						signature_count++;
						realloc(byte_array, signature_count*sizeof(int));
						byte_array[signature_count-1] = ftell(data_fd) - signature_data_size + 1;
					}
				}
			}
			else // if the previously read char was not matching signature[]..
				fread(&c, 1, 1, data_fd);
		}
	}
	// clean up..
	rewind(data_fd);
	*byte_array = 1337;
	return signature_count;
}